#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <time.h>
#include <math.h>

#include "cJSON.h"


// 服务器IP地址宏定义
#define SERVER_IP "127.0.0.1"
// 服务器端口号宏定义，需与服务器端定义的一致
#define SERVER_PORT 6666
// 发送缓冲区大小宏定义
#define MAX_SEND_BUFFER_SIZE 1024
// 接收缓冲区大小宏定义
#define MAX_RECEIVE_BUFFER_SIZE 1024
// 用户名最大长度宏定义，需与服务器端定义的一致
#define USERNAME_MAX_LENGTH 10
// 密码最大长度宏定义，需与服务器端定义的一致
#define PASSWORD_MAX_LENGTH 10



// JSON中操作类型字段名宏定义
#define JSON_TYPE_FIELD "type"
// JSON中用户名字段名宏定义
#define JSON_USERNAME_FIELD "username"
// JSON中密码字段名宏定义
#define JSON_PASSWORD_FIELD "password"
// JSON中旧密码字段名宏定义
#define JSON_OLD_PASSWORD_FIELD "oldPassword"
// JSON中新密码字段名宏定义
#define JSON_NEW_PASSWORD_FIELD "newPassword"

// 定义操作类型枚举，与服务器端保持一致
enum OperationType 
{
    REGISTER_USER = 0,
    LOGIN,
    LOOK_UP,
    EXIT
};

struct energy_value
{
	char type1[20];
	int userid[10];
	float water[10];
	float battery[10];
	float gas[10];
	struct tm time[10];
};

//cJSON发送和解析
void cJSON_send_recv(int sock,struct energy_value val)
{
	cJSON *json = NULL;
    json = cJSON_CreateObject();
	if(json == NULL)
	{
		perror("创建cJSON对象失败");
        exit(-1);
	}
	
	cJSON_AddItemToObject(json, "type1", cJSON_CreateString(val.type1));
	for(int i = 0;i<10;i++)
	{
		
		cJSON_AddItemToObject(json, "userid", cJSON_CreateNumber(val.userid[i]));
		
		cJSON_AddItemToObject(json, "water", cJSON_CreateNumber(val.water[i]));
		cJSON_AddItemToObject(json, "battery", cJSON_CreateNumber(val.battery[i]));
		cJSON_AddItemToObject(json, "gas", cJSON_CreateNumber(val.gas[i]));
		char time_str[30];
		
        sprintf(time_str, "%04d - %02d - %02d %02d:%02d:%02d", val.time[i].tm_year + 1900, val.time[i].tm_mon + 1,val.time[i].tm_mday,val.time[i].tm_hour,val.time[i].tm_min, val.time[i].tm_sec);
		
        cJSON_AddItemToObject(json, "time", cJSON_CreateString(time_str));
    }
    char *buf = NULL;
	buf = cJSON_Print(json);
	
    if (buf == NULL) 
	{
        cJSON_Delete(json);
        exit(-1);
    }
	
	//发送JSON数据
    int ret = 0;
	ret = send(sock, buf, strlen(buf), 0);
    if (ret == -1) 
	{
		perror("发送数据失败");
        close(sock);
		free(buf);
        cJSON_Delete(json);
        exit(-1);
    }
	
	puts(buf);
	free(buf);
    cJSON_Delete(json);
	
	//接收服务器JSON数据
	/*char dest[MAX_RECEIVE_BUFFER_SIZE] = {0};
    
    int ret = recv(sock, dest, MAX_RECEIVE_BUFFER_SIZE - 1, 0);
    if (ret == -1) 
	{
		perror("发送数据失败");
        close(sock);
		free(buf);
        cJSON_Delete(json);
		break;
    }
    printf("服务器响应：%s\n", dest);*/
}

// 日志记录函数，添加时间戳等信息使其更完善
void write_log(const char *msg) 
{
    time_t now = time(NULL);
    struct tm *tm_info = localtime(&now);
    char time_str[20];
    strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", tm_info);

    char log_msg[200];
    sprintf(log_msg, "[%s] %s", time_str, msg);

    FILE *fp = fopen("client.log", "a");
    if (fp!= NULL) {
        fputs(log_msg, fp);
        fclose(fp);
    }
}

int main() 
{
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock == -1) 
	{
        perror("创建套接字失败");
        return -1;
    }

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr(SERVER_IP);
    addr.sin_port = htons(SERVER_PORT);

    int ret = 0;
    ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
	if (ret == -1) 
	{
		perror("connect");
		close(sock);
		exit(-1);
	}
	
	struct energy_value val;
	memset(&val,0,sizeof(val));
	
	srand(time(NULL));
	int a = 0;
	a = rand()%10 + 1;
	
	strcpy(val.type1,"device_side");
	
	for(int i = 0;i<10;i++)
	{
		val.userid[i] = 1011;
		val.water[i] = 32.22 + a;
		val.battery[i] = 19.19 + a;
		val.gas[i] = 75.67 + a;
		time_t t = time(NULL);
		struct tm *tm_info = localtime(&t);
		val.time[i] = *tm_info; 
		
	}
	while(1)
	{	
		cJSON_send_recv(sock,val);
		sleep(30);
	}
	close(sock);
    return 0;
}
